package com.jy.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jy.pojo.SetTime;
import com.jy.pojo.Student;
import com.jy.service.StudentService;
import com.jy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin(origins = "*",maxAge = 3600)
@RequestMapping("/jy")
public class SignUpController {

    @Autowired
    @Qualifier("BookServiceImpl")
    StudentService studentService;


   // 显示所有信息
    @RequestMapping(value = "/allstu",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list() throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();
        //创建一个对象
        List<Student> list3 = studentService.signInAll();
        //将我们的对象解析成为json格式
        String str = mapper.writeValueAsString(list3);
        //由于@ResponseBody注解，这里会将str转成json格式返回；十分方便


        return str;
    }


    // 显示已签到
    @RequestMapping(value = "/readystu",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list2() throws JsonProcessingException {


        ObjectMapper mapper = new ObjectMapper();
        //创建一个对象
        List<Student> list3 = studentService.signInReady();
        //将我们的对象解析成为json格式
        String str = mapper.writeValueAsString(list3);
        //由于@ResponseBody注解，这里会将str转成json格式返回；十分方便

        return str;
    }

    // 显示迟到学生

    @RequestMapping(value = "/delaystu",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list3() throws JsonProcessingException {


        ObjectMapper mapper = new ObjectMapper();
        //创建一个对象
        List<Student> list3 = studentService.signInDelay();
        //将我们的对象解析成为json格式
        String str = mapper.writeValueAsString(list3);
        //由于@ResponseBody注解，这里会将str转成json格式返回；十分方便

        return str;
    }

    // 显示未签到学生

//    @RequestMapping(value = "/nosstu",produces = "application/json;charset=utf-8")
//    @ResponseBody
//    public String list4(){
//
//        String str = null;
//        return str;
//    }


    //注册接口

    @RequestMapping(value = "/login",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addPaper(Student student) {

        //ajax用户名重复问题

        System.out.println(student);
        boolean result = studentService.logon(student);


        //密码判断，用户名重复，验证码
        if(result==true)
            return Result.resultStatus(201,"注册失败");

        else
            return Result.resultStatus(200,"注册成功");
    }


    //登录接口

    @RequestMapping(value = "/signUp",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String signUp(String username,String password){




        return studentService.signUp(username,password);


}



    //登录接口

    @RequestMapping(value = "/signUp2",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String signUp2(String username,String password){


        boolean result = studentService.signUp2(username,password);



        //密码判断，用户名重复，验证码
        if(result==false)
            return Result.resultStatus(201,"登录失败");

        else
            return Result.resultStatus(200,"登录成功");

    }

    //签到系统接口

    @RequestMapping(value = "/signIn",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String signIn(String stu_id){



        //时间判断

        return studentService.signIn(stu_id);

    }



    //删除根据学号删除学生信息
    @RequestMapping(value = "/delete",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delete(String stu_id) throws JsonProcessingException {

        studentService.deleteStudentById(stu_id);

        return Result.resultStatus(200,"删除成功");

    }


    //修改学生信息

    @RequestMapping("/update")
    @ResponseBody
    public String update(Student student) {

        System.out.println(student);
        studentService.updateStudent(student);


        return Result.resultStatus(200,"修改成功");

    }


    //设置时间
    @RequestMapping(value = "/settime",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String setTime(String t1,String t2) throws ParseException {

        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

         SetTime.startTime=timeFormat.parse(t1);

        SetTime.endTime = timeFormat.parse(t2);



        return Result.resultStatus(200,"设置成功");

    }

    @RequestMapping(value = "/clean",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String clean() {
        studentService.deleteReady();
        studentService.deleteLate();
        return Result.resultStatus(200,"清空成功");
    }


    }
//    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
//    public Date xx

//显示所有信息

//    @RequestMapping(value = "/allstu",produces = "application/json;charset=utf-8")
//    @ResponseBody
//    public Map<String,Object> list() throws JsonProcessingException {
//
//        List<Student> list = studentService.signInAll();
//        //将我们的对象解析成为json格式
//        Map<String,Object> map = new HashMap<>();
//        map.put("data",list);
//        return map;
//    }

//显示已签到学生

//    @RequestMapping(value = "/readystu",produces = "application/json;charset=utf-8")
//    @ResponseBody
//    public Map<String,Object> list2() throws JsonProcessingException {
//
//
//        List<Student> list = studentService.signInReady();
//        //将我们的对象解析成为json格式
//        Map<String,Object> map = new HashMap<>();
//        map.put("data",list);
//
//        return map;
//    }


//@RequestMapping("/toLogon")
//    public String toAddPaper() {
//        return "logon";
//    }

//    @RequestMapping("/toSignUp")
//    public String toSignUp(){
//
//        return "signup";
//    }
//    @RequestMapping(value = "/login",produces = "application/json;charset=utf-8")
//    @ResponseBody
//    public String addPaper(Student student) {
//
//        //ajax用户名重复问题
//
//        System.out.println(student);
//        boolean result = studentService.logon(student);
//
//
//        //密码判断，用户名重复，验证码
//        if(result==true)
//            return "注册失败";
//
//        else
//            return "注册成功";
//    }